
<script>
function Stack () {
    this.items = []

    // 压栈
    Stack.prototype.push = function (element) {
        this.items.push(element)
    }

    // 从栈中获取元素
    Stack.prototype.pop = function () {
        return this.items.pop()
    }

    // 查看栈顶元素
    Stack.prototype.peek =function () {
        return this.items[this.items.length-1]
    }

    // 判断栈是否为空
    Stack.prototype.isEmpty = function () {
        return this.items.length == 0
    }

    Stack.prototype.size = function () {
        return this.items.length
    }

    Stack.prototype.toString = function () {
        let resultString = ''
        for(let i =0 ;i<this.items.length;i++){
            resultString += this.items[i] + ' '
        }
        return resultString
    }
}


let s = new Stack()

s.push(20)
s.push(10)
s.push(100)
s.push(77)

console.log(s)
console.log(s.toString())

</script>

